Ext.namespace('SLM');

SLM.DwcAdd = function(config) {

	var selectedTpl = new Ext.XTemplate(
			'<h3>{TaxonSASName}</h3>'
		, 'Source: Washington Flora Checklist'
	);

	var resultTpl = new Ext.XTemplate(
			'<tpl for="."><div class="search-item">',
					'<h3>{TaxonSASName}</h3>',
			'</div></tpl>'
	);
	

	this.cbLand = new Ext.form.ComboBox({
			fieldLabel: 'Property'
		, 	name: 'land_id'
		,	queryParam: 'value'		
		,	store: new Ext.data.Store({
					proxy: new Ext.data.HttpProxy({url: Config.baseUrl + 'resources/api/app.php'})
				,	baseParams: { cmd: "land-getlist", field: 'title' }
				, 	reader: new Ext.data.JsonReader({id: 'name', root: 'records'}, [ {name: 'name', mapping: 'value'}, 'land_id' ])
			})
		,	displayField: 'name'
		,	typeAhead: false
		,	hideTrigger2: false
		, 	triggerAction: 'all'
		,	editable: false
		,	value: ''
		,	width: 265
		,	valueField: 'land_id'
		, 	hiddenName: 'land_id'
	});
				
	this.cbCol = new Ext.form.ComboBox({
			store: new Ext.data.JsonStore({
					url: Config.baseUrl + 'resources/api/app.php'
				,	baseParams: {
						cmd: 'waflora-getlist'
					}
				,	root: 'records'
				,	id: 'ID'
				, fields: [
							{name: 'TaxonSASName'}
						,	{name: 'ID'}
					]
			})
		,	displayField: 'TaxonSASName'
		,	typeAhead: false
		,	loadingText: 'Searching Washington Flora...'
		,	width: 265
		,	pageSize: 500
		,	hideTrigger: true
		,	queryParam: 'name'
		,	tpl: resultTpl
		,	minChars: 2
		,	itemSelector: 'div.search-item'			
		,	valueField: 'ID'
		, 	hiddenName: 'ID'
		,	ownerCt: this
		,	listeners: {
				select: function( cb, record, i ) {
					selectedTpl.overwrite("waflora-info", record.data );
					Ext.get("waflora-info").show();
					this.ownerCt.specimen = record.data;
					this.ownerCt.btnAdd.enable();
				}
			}
	});

	this.btnAdd = new Ext.Button({
			text: 'Add Species'
		,	scope: this
		,	handler: this.addSpecies
		,	disabled: true
	});
	
	Ext.apply(this, config, {
			title: 'Add Species'
		,	iconCls: 'icon_events_add'
		,	width: 300
		,	height: 350
		,	modal: true
		,	resizable: false
		,	onEsc: Ext.emptyFn
		,	autoScroll: true
		,	bodyStyle: 'padding: 10px'
		,	html: 'To add a species type in the genus and all or part of the species name.<br/><br/><div>Property: <div id="cb-waflora-land_id"></div><br/><div>Search Washington Flora: <div id="cb-col-search"></div><br/>Results:<div id="waflora-info"></div>'
		,	listeners: {
				show: function() {
					this.cbCol.render("cb-col-search");
					this.cbLand.render("cb-waflora-land_id");
				}
			}
		,	buttons: [ this.btnAdd, {
					text: 'Close'
				,	scope: this	
				,	handler: function(){
						this.close();
					}
			}]
	});

	SLM.DwcAdd.superclass.constructor.apply(this, arguments);

};

Ext.extend( SLM.DwcAdd, Ext.Window, {		 
		addSpecies: function() {
			Ext.Ajax.request({
					url: Config.baseUrl + 'resources/api/app.php'
				,	scope: this
				,	success: function( response, request ) {
						var r = Ext.decode( response.responseText );
						if (r.success) {
							this.btnAdd.disable();
							this.cbCol.reset();
							this.cbCol.focus();
							Ext.get('waflora-info').update('<div class="col-success">Species Successfully added to Property</div>').pause(2).fadeOut();
						}
						
					}
				,	params: { 
							cmd: 'dwc-update'
						,	col_id: this.cbCol.getValue()
						,	land_id: this.cbLand.getValue()
					}
			});
		}	
});